<template>
  <div style="width: 50%;border: 1px solid;">
    <div class="card" style="padding: 25px 30px;background: linear-gradient(to bottom, #f5f3f2, #92bdce)">
      <div style="font-size: 20px;color: red;display: flex;align-items: center;justify-content: center">
        <span style="margin-bottom: 20px">个人资料修改</span>
      </div>
      <el-form :model="form.user" label-width="100px" style="padding-right: 50px">
        <el-form-item label="账号">
          <el-input :prefix-icon="User" v-model="data.user.username" autocomplete="off" disabled />
        </el-form-item>
        <el-form-item label="名称">
          <el-input :prefix-icon="UserFilled" v-model="form.user.realname" autocomplete="off" />
        </el-form-item>
        <el-form-item label="邮件">
          <el-input :prefix-icon="Message" v-model="form.user.email" autocomplete="off" />
        </el-form-item>
        <el-form-item label="电话">
          <el-input :prefix-icon="PhoneFilled" v-model="form.user.phone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="职称" prop="title">
          <el-select v-model="form.user.title" placeholder="请选择职称" style="width: 100%" disabled>
            <template #prefix>
              <el-icon>
                <SuitcaseLine />
              </el-icon>
            </template>
            <el-option label="讲师" value="讲师"></el-option>
            <el-option label="副教授" value="副教授"></el-option>
            <el-option label="教授" value="教授"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属学院" prop="collegeName">
          <el-select style="width: 100%" v-model="data.user.collegeId" disabled>
            <template #prefix>
              <el-icon>
                <School />
              </el-icon>
            </template>
            <el-option v-for="item in data.collegeList" :key="item.collegeId" :label="item.name"
              :value="item.collegeId" />
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select style="width: 100%" v-model="data.user.role" disabled>
            <template #prefix>
              <el-icon>
                <Stamp />
              </el-icon>
            </template>
            <el-option value="SUPER_ADMIN" label="系统管理员"></el-option>
            <el-option value="ADMIN" label="院管理员"></el-option>
            <el-option value="TEACHER" label="教师管理员"></el-option>
            <el-option value="STUDENT" label="学生"></el-option>
          </el-select>
        </el-form-item>
        <div style="text-align: center">
          <el-button style="padding: 10px 25px" type="primary" @click="save">保存个人资料</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { onMounted, reactive } from "vue"
import { ElMessage } from "element-plus";
import { Message, PhoneFilled, School, Stamp, User, UserFilled } from "@element-plus/icons-vue";
import alovaInstance from "@/utils/request.js";
import { SelectTeacherByid, updateTeacher } from "@/api/teacher/teacher";


const data = reactive({
  user: JSON.parse(localStorage.getItem('credit-system-user') || '{}'),
  collegeData: [],//学院
})

const form = reactive({
  teaId: '',
  user: {}
})

const save = () => {
  form.user.updateBy = data.user.teaId
  updateTeacher(form.user).then(() => {
    ElMessage.success('修改成功');
    localStorage.setItem('credit-system-user', JSON.stringify(form.user))
    emit('updateUser')
    setTimeout(() => { location.reload(); }, 500);
  }).catch(err => {
    ElMessage.error('修改失败：' + err.message);
  });
}


const emit = defineEmits(["updateUser"])

const loadCollegeList = () => {
  alovaInstance.Get('/sysAdminCollege/selectAll').then(res => {
    if (res.code === '200') {
      data.collegeList = res.data
    }
  })
}

const getTeacher = async () => {
  try {
    if (!data.user.teaId) {
      console.log('教师ID不存在');
      return;
    }

    // 发送请求并等待响应
    const res = await SelectTeacherByid(data.user.teaId);

    if (res.code === '200') {
      // 正确赋值教师数据
      form.user = res.data;
      console.log('教师信息', form.user);
    } else {
      ElMessage.error(res.message || '查询教师信息失败');
    }
  } catch (error) {
    console.error('查询教师信息异常', error);
    ElMessage.error('查询教师信息失败，请稍后再试');
  }
};



onMounted(async () => {
  await loadCollegeList();
  console.log("组件已挂载");
  await getTeacher();
});
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  text-align: center;
}
</style>